package cn.phecda.boot.tenant.test.table.mybatis;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p> TODO </p>
 *
 * @author : wanpinwei
 */
@RequestMapping("/tenant-table/persons/mybatis")
@RestController
@Transactional
@RequiredArgsConstructor
public class PersonTenantMybatisController {
    private static int index = 0;
    private final PersonTenantMybatisService personTenantMybatisService;


    @GetMapping
    public List<PersonTenantMybatisEntity> all() {
        return personTenantMybatisService.getBaseMapper().selectList(new QueryWrapper<>());
    }

    @GetMapping("/id")
    public PersonTenantMybatisEntity id(@RequestParam("id")  String id) {
        return personTenantMybatisService.getBaseMapper().selectById(id);
    }

    @PostMapping
    public PersonTenantMybatisEntity save(@RequestParam("name") String name) {
        String newName = name + ++index;
        return personTenantMybatisService.save(newName);
    }

    @PutMapping
    public void update(@RequestParam("id") String id, @RequestParam("name") String name) {
        personTenantMybatisService.update(id, name);
    }

    @PostMapping("/async")
    public void asnyc(@RequestParam("name") String name) {
        String newName = name + ++index;
        personTenantMybatisService.asyncSave(newName);
    }

    @DeleteMapping
    public void delete(@RequestParam("id")  String id) {
        personTenantMybatisService.getBaseMapper().deleteById(id);
    }
}
